Assignment 3

import pandas as pd
import geopandas as gpd
import numpy as np
from matplotlib import pyplot as plt

# See lots of columns
pd.options.display.max_columns = 999

# Hide warnings due to issue in shapely package 
# See: https://github.com/shapely/shapely/issues/1345
np.seterr(invalid="ignore");

%matplotlib inline

This assignment will contain two parts:

  1. Exploring evictions and code violations in Philadelphia
  2. Comparing the NDVI in Philadelphia

Hansen Xie

Part 1: Exploring Evictions and Code Violations in Philadelphia

In this assignment, we’ll explore spatial trends evictions in Philadelphia using data from the Eviction Lab and building code violations using data from OpenDataPhilly.

We’ll be exploring the idea that evictions can occur as retaliation against renters for reporting code violations. Spatial correlations between evictions and code violations from the City’s Licenses and Inspections department can offer some insight into this question.

A couple of interesting background readings: - HuffPost article - PlanPhilly article

1.1 Explore Eviction Lab Data

The Eviction Lab built the first national database for evictions. If you aren’t familiar with the project, you can explore their website: https://evictionlab.org/

1.1.1 Read data using geopandas

The first step is to read the eviction data by census tract using geopandas. The data for all of Pennsylvania by census tract is available in the data/ folder in a GeoJSON format.

Load the data file “PA-tracts.geojson” using geopandas

Note: If you’d like to see all columns in the data frame, you can increase the max number of columns using pandas display options:

pa_tracts = gpd.read_file("data/PA-tracts.geojson")
pa_tracts.head()
GEOID west south east north n pl p-00 pr-00 roh-00 pro-00 mgr-00 mhi-00 mpv-00 rb-00 pw-00 paa-00 ph-00 pai-00 pa-00 pnp-00 pm-00 po-00 ef-00 e-00 er-00 efr-00 lf-00 imputed-00 subbed-00 p-01 pr-01 roh-01 pro-01 mgr-01 mhi-01 mpv-01 rb-01 pw-01 paa-01 ph-01 pai-01 pa-01 pnp-01 pm-01 po-01 ef-01 e-01 er-01 efr-01 lf-01 imputed-01 subbed-01 p-02 pr-02 roh-02 pro-02 mgr-02 mhi-02 mpv-02 rb-02 pw-02 paa-02 ph-02 pai-02 pa-02 pnp-02 pm-02 po-02 ef-02 e-02 er-02 efr-02 lf-02 imputed-02 subbed-02 p-03 pr-03 roh-03 pro-03 mgr-03 mhi-03 mpv-03 rb-03 pw-03 paa-03 ph-03 pai-03 pa-03 pnp-03 pm-03 po-03 ef-03 e-03 er-03 efr-03 lf-03 imputed-03 subbed-03 p-04 pr-04 roh-04 pro-04 mgr-04 mhi-04 mpv-04 rb-04 pw-04 paa-04 ph-04 pai-04 pa-04 pnp-04 pm-04 po-04 ef-04 e-04 er-04 efr-04 lf-04 imputed-04 subbed-04 p-05 pr-05 roh-05 pro-05 mgr-05 mhi-05 mpv-05 rb-05 pw-05 paa-05 ph-05 pai-05 pa-05 pnp-05 pm-05 po-05 ef-05 e-05 er-05 efr-05 lf-05 imputed-05 subbed-05 p-06 pr-06 roh-06 pro-06 mgr-06 mhi-06 mpv-06 rb-06 pw-06 paa-06 ph-06 pai-06 pa-06 pnp-06 pm-06 po-06 ef-06 e-06 er-06 efr-06 lf-06 imputed-06 subbed-06 p-07 pr-07 roh-07 pro-07 mgr-07 mhi-07 mpv-07 rb-07 pw-07 paa-07 ph-07 pai-07 pa-07 pnp-07 pm-07 po-07 ef-07 e-07 er-07 efr-07 lf-07 imputed-07 subbed-07 p-08 pr-08 roh-08 pro-08 mgr-08 mhi-08 mpv-08 rb-08 pw-08 paa-08 ph-08 pai-08 pa-08 pnp-08 pm-08 po-08 ef-08 e-08 er-08 efr-08 lf-08 imputed-08 subbed-08 p-09 pr-09 roh-09 pro-09 mgr-09 mhi-09 mpv-09 rb-09 pw-09 paa-09 ph-09 pai-09 pa-09 pnp-09 pm-09 po-09 ef-09 e-09 er-09 efr-09 lf-09 imputed-09 subbed-09 p-10 pr-10 roh-10 pro-10 mgr-10 mhi-10 mpv-10 rb-10 pw-10 paa-10 ph-10 pai-10 pa-10 pnp-10 pm-10 po-10 ef-10 e-10 er-10 efr-10 lf-10 imputed-10 subbed-10 p-11 pr-11 roh-11 pro-11 mgr-11 mhi-11 mpv-11 rb-11 pw-11 paa-11 ph-11 pai-11 pa-11 pnp-11 pm-11 po-11 ef-11 e-11 er-11 efr-11 lf-11 imputed-11 subbed-11 p-12 pr-12 roh-12 pro-12 mgr-12 mhi-12 mpv-12 rb-12 pw-12 paa-12 ph-12 pai-12 pa-12 pnp-12 pm-12 po-12 ef-12 e-12 er-12 efr-12 lf-12 imputed-12 subbed-12 p-13 pr-13 roh-13 pro-13 mgr-13 mhi-13 mpv-13 rb-13 pw-13 paa-13 ph-13 pai-13 pa-13 pnp-13 pm-13 po-13 ef-13 e-13 er-13 efr-13 lf-13 imputed-13 subbed-13 p-14 pr-14 roh-14 pro-14 mgr-14 mhi-14 mpv-14 rb-14 pw-14 paa-14 ph-14 pai-14 pa-14 pnp-14 pm-14 po-14 ef-14 e-14 er-14 efr-14 lf-14 imputed-14 subbed-14 p-15 pr-15 roh-15 pro-15 mgr-15 mhi-15 mpv-15 rb-15 pw-15 paa-15 ph-15 pai-15 pa-15 pnp-15 pm-15 po-15 ef-15 e-15 er-15 efr-15 lf-15 imputed-15 subbed-15 p-16 pr-16 roh-16 pro-16 mgr-16 mhi-16 mpv-16 rb-16 pw-16 paa-16 ph-16 pai-16 pa-16 pnp-16 pm-16 po-16 ef-16 e-16 er-16 efr-16 lf-16 imputed-16 subbed-16 geometry
0 42003412002 -80.1243 40.5422 -80.0640 40.5890 4120.02 Allegheny County, Pennsylvania 4748.59 0.88 58.0 3.66 949.31 89226.06 195358.33 22.82 94.89 0.84 0.43 0.06 2.83 0.33 0.62 0.00 1.0 1.0 1.73 1.73 0.0 0.0 0.0 4748.59 0.88 59.0 3.66 949.31 89226.06 195358.33 22.82 94.89 0.84 0.43 0.06 2.83 0.33 0.62 0.00 0.0 0.0 0.00 0.00 0.0 0.0 0.0 4748.59 0.88 60.0 3.66 949.31 89226.06 195358.33 22.82 94.89 0.84 0.43 0.06 2.83 0.33 0.62 0.00 3.0 0.0 0.00 4.99 0.0 0.0 0.0 4748.59 0.88 61.0 3.66 949.31 89226.06 195358.33 22.82 94.89 0.84 0.43 0.06 2.83 0.33 0.62 0.00 0.0 0.0 0.00 0.00 0.0 0.0 0.0 4748.59 0.88 62.0 3.66 949.31 89226.06 195358.33 22.82 94.89 0.84 0.43 0.06 2.83 0.33 0.62 0.00 0.0 0.0 0.00 0.00 0.0 0.0 0.0 4525.07 0.93 63.0 5.83 799.06 117867.25 267511.43 13.86 89.33 1.42 2.65 0.00 6.41 0.0 0.00 0.19 1.0 0.0 0.00 1.58 0.0 0.0 0.0 4525.07 0.93 65.0 5.83 799.06 117867.25 267511.43 13.86 89.33 1.42 2.65 0.00 6.41 0.0 0.00 0.19 0.0 0.0 0.00 0.00 0.0 0.0 0.0 4525.07 0.93 66.0 5.83 799.06 117867.25 267511.43 13.86 89.33 1.42 2.65 0.00 6.41 0.0 0.00 0.19 0.0 0.0 0.00 0.00 0.0 0.0 1.0 4525.07 0.93 67.0 5.83 799.06 117867.25 267511.43 13.86 89.33 1.42 2.65 0.00 6.41 0.0 0.00 0.19 5.0 4.0 5.99 7.49 0.0 0.0 1.0 4525.07 0.93 68.0 5.83 799.06 117867.25 267511.43 13.86 89.33 1.42 2.65 0.00 6.41 0.0 0.00 0.19 2.0 1.0 1.47 2.95 0.0 0.0 1.0 4865.0 0.00 69.0 4.08 838.0 119583.0 280900.0 16.9 91.57 0.84 1.03 0.00 5.80 0.12 0.55 0.08 1.0 0.0 0.00 1.45 0.0 0.0 1.0 5362.0 0.46 71.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 2.0 0.0 0.00 2.82 0.0 0.0 1.0 5362.0 0.46 73.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 2.0 0.0 0.00 2.75 0.0 0.0 1.0 5362.0 0.46 75.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 0.0 0.0 0.00 0.00 0.0 0.0 1.0 5362.0 0.46 76.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 1.0 0.0 0.00 1.31 0.0 0.0 1.0 5362.0 0.46 78.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 1.0 0.0 0.00 1.28 0.0 0.0 1.0 5362.0 0.46 80.0 3.28 1313.0 118611.0 302900.0 50.0 91.53 0.50 0.97 0.06 6.94 0.0 0.00 0.0 0.0 0.0 0.00 0.00 1.0 0.0 1.0 MULTIPOLYGON (((-80.06670 40.58401, -80.06655 ...
1 42003413100 -80.0681 40.5850 -79.9906 40.6143 4131 Allegheny County, Pennsylvania 6771.01 3.47 729.0 28.75 674.10 75492.65 193726.88 21.39 91.61 1.81 0.94 0.03 4.85 0.01 0.66 0.07 10.0 4.0 0.55 1.37 0.0 0.0 0.0 6771.01 3.47 724.0 28.75 674.10 75492.65 193726.88 21.39 91.61 1.81 0.94 0.03 4.85 0.01 0.66 0.07 16.0 11.0 1.52 2.21 0.0 0.0 0.0 6771.01 3.47 719.0 28.75 674.10 75492.65 193726.88 21.39 91.61 1.81 0.94 0.03 4.85 0.01 0.66 0.07 22.0 17.0 2.37 3.06 0.0 0.0 0.0 6771.01 3.47 713.0 28.75 674.10 75492.65 193726.88 21.39 91.61 1.81 0.94 0.03 4.85 0.01 0.66 0.07 14.0 10.0 1.40 1.96 0.0 0.0 0.0 6771.01 3.47 708.0 28.75 674.10 75492.65 193726.88 21.39 91.61 1.81 0.94 0.03 4.85 0.01 0.66 0.07 19.0 8.0 1.13 2.68 0.0 0.0 0.0 5648.32 2.06 703.0 31.24 830.01 86853.22 278013.85 26.67 89.57 1.87 2.30 0.00 3.88 0.0 2.38 0.00 31.0 12.0 1.71 4.41 0.0 0.0 0.0 5648.32 2.06 697.0 31.24 830.01 86853.22 278013.85 26.67 89.57 1.87 2.30 0.00 3.88 0.0 2.38 0.00 22.0 14.0 2.01 3.15 0.0 0.0 0.0 5648.32 2.06 692.0 31.24 830.01 86853.22 278013.85 26.67 89.57 1.87 2.30 0.00 3.88 0.0 2.38 0.00 34.0 4.0 0.58 4.91 0.0 0.0 1.0 5648.32 2.06 687.0 31.24 830.01 86853.22 278013.85 26.67 89.57 1.87 2.30 0.00 3.88 0.0 2.38 0.00 43.0 6.0 0.87 6.26 0.0 0.0 1.0 5648.32 2.06 681.0 31.24 830.01 86853.22 278013.85 26.67 89.57 1.87 2.30 0.00 3.88 0.0 2.38 0.00 24.0 3.0 0.44 3.52 0.0 0.0 1.0 6609.0 2.68 676.0 26.19 865.0 97264.0 271600.0 23.3 87.80 1.72 1.74 0.15 7.49 0.11 0.86 0.12 37.0 1.0 0.15 5.47 0.0 0.0 1.0 7223.0 4.26 687.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 49.0 3.0 0.44 7.14 0.0 0.0 1.0 7223.0 4.26 697.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 29.0 5.0 0.72 4.16 0.0 0.0 1.0 7223.0 4.26 708.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 23.0 3.0 0.42 3.25 0.0 0.0 1.0 7223.0 4.26 718.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 21.0 2.0 0.28 2.92 0.0 0.0 1.0 7223.0 4.26 729.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 16.0 2.0 0.27 2.20 0.0 0.0 1.0 7223.0 4.26 739.0 27.37 943.0 114036.0 286600.0 21.7 77.24 3.88 1.27 0.00 16.02 0.0 1.59 0.0 12.0 2.0 0.27 1.62 1.0 0.0 1.0 MULTIPOLYGON (((-80.06806 40.61254, -80.05452 ...
2 42003413300 -80.0657 40.5527 -80.0210 40.5721 4133 Allegheny County, Pennsylvania 5044.59 2.99 119.0 6.68 938.08 60019.14 131521.90 28.36 97.15 0.37 0.49 0.12 1.61 0.00 0.26 0.00 2.0 1.0 0.84 1.68 0.0 0.0 0.0 5044.59 2.99 125.0 6.68 938.08 60019.14 131521.90 28.36 97.15 0.37 0.49 0.12 1.61 0.00 0.26 0.00 2.0 2.0 1.60 1.60 0.0 0.0 0.0 5044.59 2.99 131.0 6.68 938.08 60019.14 131521.90 28.36 97.15 0.37 0.49 0.12 1.61 0.00 0.26 0.00 0.0 0.0 0.00 0.00 0.0 0.0 0.0 5044.59 2.99 137.0 6.68 938.08 60019.14 131521.90 28.36 97.15 0.37 0.49 0.12 1.61 0.00 0.26 0.00 4.0 0.0 0.00 2.92 0.0 0.0 0.0 5044.59 2.99 143.0 6.68 938.08 60019.14 131521.90 28.36 97.15 0.37 0.49 0.12 1.61 0.00 0.26 0.00 2.0 0.0 0.00 1.40 0.0 0.0 0.0 4562.01 2.50 149.0 4.97 680.77 75425.61 162394.01 23.93 93.87 1.56 0.81 0.00 3.76 0.0 0.00 0.00 4.0 1.0 0.67 2.69 0.0 0.0 0.0 4562.01 2.50 154.0 4.97 680.77 75425.61 162394.01 23.93 93.87 1.56 0.81 0.00 3.76 0.0 0.00 0.00 5.0 5.0 3.24 3.24 0.0 0.0 0.0 4562.01 2.50 160.0 4.97 680.77 75425.61 162394.01 23.93 93.87 1.56 0.81 0.00 3.76 0.0 0.00 0.00 1.0 0.0 0.00 0.62 0.0 0.0 1.0 4562.01 2.50 166.0 4.97 680.77 75425.61 162394.01 23.93 93.87 1.56 0.81 0.00 3.76 0.0 0.00 0.00 2.0 0.0 0.00 1.20 0.0 0.0 1.0 4562.01 2.50 172.0 4.97 680.77 75425.61 162394.01 23.93 93.87 1.56 0.81 0.00 3.76 0.0 0.00 0.00 3.0 1.0 0.58 1.74 0.0 0.0 1.0 4742.0 2.54 178.0 9.59 936.0 84688.0 170400.0 25.0 93.72 0.97 0.78 0.11 3.52 0.00 0.76 0.15 6.0 2.0 1.12 3.37 0.0 0.0 1.0 4744.0 2.52 182.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 0.0 0.0 0.00 0.00 0.0 0.0 1.0 4744.0 2.52 187.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 6.0 1.0 0.54 3.21 0.0 0.0 1.0 4744.0 2.52 191.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 1.0 0.0 0.00 0.52 0.0 0.0 1.0 4744.0 2.52 195.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 1.0 1.0 0.51 0.51 0.0 0.0 1.0 4744.0 2.52 200.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 2.0 0.0 0.00 1.00 0.0 0.0 1.0 4744.0 2.52 204.0 10.96 780.0 78630.0 174200.0 20.7 91.74 1.52 2.21 0.19 3.39 0.0 0.95 0.0 4.0 1.0 0.49 1.96 1.0 0.0 1.0 MULTIPOLYGON (((-80.03822 40.55349, -80.04368 ...
3 42003416000 -79.8113 40.5440 -79.7637 40.5630 4160 Allegheny County, Pennsylvania 1775.93 4.99 121.0 15.30 557.90 39073.61 77316.48 17.60 99.50 0.06 0.11 0.00 0.00 0.00 0.33 0.00 1.0 1.0 0.83 0.83 0.0 0.0 0.0 1775.93 4.99 123.0 15.30 557.90 39073.61 77316.48 17.60 99.50 0.06 0.11 0.00 0.00 0.00 0.33 0.00 3.0 3.0 2.44 2.44 0.0 0.0 0.0 1775.93 4.99 125.0 15.30 557.90 39073.61 77316.48 17.60 99.50 0.06 0.11 0.00 0.00 0.00 0.33 0.00 3.0 3.0 2.41 2.41 0.0 0.0 0.0 1775.93 4.99 127.0 15.30 557.90 39073.61 77316.48 17.60 99.50 0.06 0.11 0.00 0.00 0.00 0.33 0.00 4.0 3.0 2.37 3.16 0.0 0.0 0.0 1775.93 4.99 128.0 15.30 557.90 39073.61 77316.48 17.60 99.50 0.06 0.11 0.00 0.00 0.00 0.33 0.00 2.0 1.0 0.78 1.56 0.0 0.0 0.0 1569.13 7.82 130.0 18.14 546.12 43434.32 96418.92 27.51 98.74 0.00 0.00 0.13 0.00 0.0 1.13 0.00 7.0 6.0 4.61 5.38 0.0 0.0 0.0 1569.13 7.82 132.0 18.14 546.12 43434.32 96418.92 27.51 98.74 0.00 0.00 0.13 0.00 0.0 1.13 0.00 7.0 4.0 3.03 5.31 0.0 0.0 0.0 1569.13 7.82 134.0 18.14 546.12 43434.32 96418.92 27.51 98.74 0.00 0.00 0.13 0.00 0.0 1.13 0.00 0.0 0.0 0.00 0.00 0.0 0.0 1.0 1569.13 7.82 135.0 18.14 546.12 43434.32 96418.92 27.51 98.74 0.00 0.00 0.13 0.00 0.0 1.13 0.00 2.0 2.0 1.48 1.48 0.0 0.0 1.0 1569.13 7.82 137.0 18.14 546.12 43434.32 96418.92 27.51 98.74 0.00 0.00 0.13 0.00 0.0 1.13 0.00 5.0 4.0 2.92 3.64 0.0 0.0 1.0 1636.0 5.00 139.0 17.48 766.0 45491.0 95100.0 26.4 99.08 0.18 0.61 0.00 0.06 0.00 0.06 0.00 6.0 4.0 2.88 4.32 0.0 0.0 1.0 1643.0 6.16 141.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 1.0 1.0 0.71 0.71 0.0 0.0 1.0 1643.0 6.16 144.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 3.0 3.0 2.09 2.09 0.0 0.0 1.0 1643.0 6.16 146.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 4.0 2.0 1.37 2.74 0.0 0.0 1.0 1643.0 6.16 148.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 5.0 2.0 1.35 3.37 0.0 0.0 1.0 1643.0 6.16 151.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 3.0 2.0 1.33 1.99 0.0 0.0 1.0 1643.0 6.16 153.0 23.59 680.0 48438.0 94900.0 19.7 99.45 0.00 0.00 0.00 0.00 0.0 0.55 0.0 1.0 1.0 0.65 0.65 1.0 0.0 1.0 MULTIPOLYGON (((-79.76595 40.55092, -79.76542 ...
4 42003417200 -79.7948 40.5341 -79.7642 40.5443 4172 Allegheny County, Pennsylvania 1428.03 11.95 321.0 48.27 409.00 34306.14 66400.38 22.10 98.46 0.63 0.28 0.07 0.07 0.00 0.35 0.14 9.0 7.0 2.18 2.80 0.0 0.0 0.0 1428.03 11.95 322.0 48.27 409.00 34306.14 66400.38 22.10 98.46 0.63 0.28 0.07 0.07 0.00 0.35 0.14 12.0 4.0 1.24 3.73 0.0 0.0 0.0 1428.03 11.95 323.0 48.27 409.00 34306.14 66400.38 22.10 98.46 0.63 0.28 0.07 0.07 0.00 0.35 0.14 17.0 9.0 2.79 5.27 0.0 0.0 0.0 1428.03 11.95 324.0 48.27 409.00 34306.14 66400.38 22.10 98.46 0.63 0.28 0.07 0.07 0.00 0.35 0.14 12.0 7.0 2.16 3.71 0.0 0.0 0.0 1428.03 11.95 325.0 48.27 409.00 34306.14 66400.38 22.10 98.46 0.63 0.28 0.07 0.07 0.00 0.35 0.14 12.0 9.0 2.77 3.70 0.0 0.0 0.0 1345.03 9.52 326.0 41.37 716.00 40257.36 81200.46 25.00 90.63 0.00 7.43 0.00 0.00 0.0 1.93 0.00 11.0 11.0 3.38 3.38 0.0 0.0 0.0 1345.03 9.52 326.0 41.37 716.00 40257.36 81200.46 25.00 90.63 0.00 7.43 0.00 0.00 0.0 1.93 0.00 14.0 12.0 3.68 4.29 0.0 0.0 0.0 1345.03 9.52 327.0 41.37 716.00 40257.36 81200.46 25.00 90.63 0.00 7.43 0.00 0.00 0.0 1.93 0.00 1.0 0.0 0.00 0.31 0.0 0.0 1.0 1345.03 9.52 328.0 41.37 716.00 40257.36 81200.46 25.00 90.63 0.00 7.43 0.00 0.00 0.0 1.93 0.00 1.0 0.0 0.00 0.30 0.0 0.0 1.0 1345.03 9.52 329.0 41.37 716.00 40257.36 81200.46 25.00 90.63 0.00 7.43 0.00 0.00 0.0 1.93 0.00 9.0 2.0 0.61 2.73 0.0 0.0 1.0 1260.0 1.32 330.0 52.13 739.0 39934.0 96500.0 29.3 96.43 1.19 0.79 0.08 0.08 0.00 1.27 0.16 11.0 6.0 1.82 3.33 0.0 0.0 1.0 1253.0 8.36 336.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 11.0 5.0 1.49 3.27 0.0 0.0 1.0 1253.0 8.36 343.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 6.0 4.0 1.17 1.75 0.0 0.0 1.0 1253.0 8.36 349.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 6.0 5.0 1.43 1.72 0.0 0.0 1.0 1253.0 8.36 355.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 13.0 8.0 2.25 3.66 0.0 0.0 1.0 1253.0 8.36 362.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 8.0 6.0 1.66 2.21 0.0 0.0 1.0 1253.0 8.36 368.0 46.57 625.0 32614.0 78300.0 33.0 97.69 0.96 1.36 0.00 0.00 0.0 0.00 0.0 7.0 3.0 0.82 1.90 1.0 0.0 1.0 MULTIPOLYGON (((-79.77114 40.54415, -79.76417 ...
type(pa_tracts)
geopandas.geodataframe.GeoDataFrame

1.1.2 Explore and trim the data

We will need to trim data to Philadelphia only. Take a look at the data dictionary for the descriptions of the various columns in top-level repository folder: eviction_lab_data_dictionary.txt

Note: the column names are shortened — see the end of the above file for the abbreviations. The numbers at the end of the columns indicate the years. For example, e-16 is the number of evictions in 2016.

Take a look at the individual columns and trim to census tracts in Philadelphia. (Hint: Philadelphia is both a city and a county).

philly_tracts = pa_tracts[pa_tracts['pl'] == 'Philadelphia County, Pennsylvania']
philly_tracts.head()
GEOID west south east north n pl p-00 pr-00 roh-00 pro-00 mgr-00 mhi-00 mpv-00 rb-00 pw-00 paa-00 ph-00 pai-00 pa-00 pnp-00 pm-00 po-00 ef-00 e-00 er-00 efr-00 lf-00 imputed-00 subbed-00 p-01 pr-01 roh-01 pro-01 mgr-01 mhi-01 mpv-01 rb-01 pw-01 paa-01 ph-01 pai-01 pa-01 pnp-01 pm-01 po-01 ef-01 e-01 er-01 efr-01 lf-01 imputed-01 subbed-01 p-02 pr-02 roh-02 pro-02 mgr-02 mhi-02 mpv-02 rb-02 pw-02 paa-02 ph-02 pai-02 pa-02 pnp-02 pm-02 po-02 ef-02 e-02 er-02 efr-02 lf-02 imputed-02 subbed-02 p-03 pr-03 roh-03 pro-03 mgr-03 mhi-03 mpv-03 rb-03 pw-03 paa-03 ph-03 pai-03 pa-03 pnp-03 pm-03 po-03 ef-03 e-03 er-03 efr-03 lf-03 imputed-03 subbed-03 p-04 pr-04 roh-04 pro-04 mgr-04 mhi-04 mpv-04 rb-04 pw-04 paa-04 ph-04 pai-04 pa-04 pnp-04 pm-04 po-04 ef-04 e-04 er-04 efr-04 lf-04 imputed-04 subbed-04 p-05 pr-05 roh-05 pro-05 mgr-05 mhi-05 mpv-05 rb-05 pw-05 paa-05 ph-05 pai-05 pa-05 pnp-05 pm-05 po-05 ef-05 e-05 er-05 efr-05 lf-05 imputed-05 subbed-05 p-06 pr-06 roh-06 pro-06 mgr-06 mhi-06 mpv-06 rb-06 pw-06 paa-06 ph-06 pai-06 pa-06 pnp-06 pm-06 po-06 ef-06 e-06 er-06 efr-06 lf-06 imputed-06 subbed-06 p-07 pr-07 roh-07 pro-07 mgr-07 mhi-07 mpv-07 rb-07 pw-07 paa-07 ph-07 pai-07 pa-07 pnp-07 pm-07 po-07 ef-07 e-07 er-07 efr-07 lf-07 imputed-07 subbed-07 p-08 pr-08 roh-08 pro-08 mgr-08 mhi-08 mpv-08 rb-08 pw-08 paa-08 ph-08 pai-08 pa-08 pnp-08 pm-08 po-08 ef-08 e-08 er-08 efr-08 lf-08 imputed-08 subbed-08 p-09 pr-09 roh-09 pro-09 mgr-09 mhi-09 mpv-09 rb-09 pw-09 paa-09 ph-09 pai-09 pa-09 pnp-09 pm-09 po-09 ef-09 e-09 er-09 efr-09 lf-09 imputed-09 subbed-09 p-10 pr-10 roh-10 pro-10 mgr-10 mhi-10 mpv-10 rb-10 pw-10 paa-10 ph-10 pai-10 pa-10 pnp-10 pm-10 po-10 ef-10 e-10 er-10 efr-10 lf-10 imputed-10 subbed-10 p-11 pr-11 roh-11 pro-11 mgr-11 mhi-11 mpv-11 rb-11 pw-11 paa-11 ph-11 pai-11 pa-11 pnp-11 pm-11 po-11 ef-11 e-11 er-11 efr-11 lf-11 imputed-11 subbed-11 p-12 pr-12 roh-12 pro-12 mgr-12 mhi-12 mpv-12 rb-12 pw-12 paa-12 ph-12 pai-12 pa-12 pnp-12 pm-12 po-12 ef-12 e-12 er-12 efr-12 lf-12 imputed-12 subbed-12 p-13 pr-13 roh-13 pro-13 mgr-13 mhi-13 mpv-13 rb-13 pw-13 paa-13 ph-13 pai-13 pa-13 pnp-13 pm-13 po-13 ef-13 e-13 er-13 efr-13 lf-13 imputed-13 subbed-13 p-14 pr-14 roh-14 pro-14 mgr-14 mhi-14 mpv-14 rb-14 pw-14 paa-14 ph-14 pai-14 pa-14 pnp-14 pm-14 po-14 ef-14 e-14 er-14 efr-14 lf-14 imputed-14 subbed-14 p-15 pr-15 roh-15 pro-15 mgr-15 mhi-15 mpv-15 rb-15 pw-15 paa-15 ph-15 pai-15 pa-15 pnp-15 pm-15 po-15 ef-15 e-15 er-15 efr-15 lf-15 imputed-15 subbed-15 p-16 pr-16 roh-16 pro-16 mgr-16 mhi-16 mpv-16 rb-16 pw-16 paa-16 ph-16 pai-16 pa-16 pnp-16 pm-16 po-16 ef-16 e-16 er-16 efr-16 lf-16 imputed-16 subbed-16 geometry
435 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.00 1.40 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.00 1.40 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.00 1.40 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.00 1.40 0.11 25.0 21.0 1.51 1.80 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.00 1.40 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.40 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.40 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.70 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.40 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.40 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.40 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.70 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.00 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.00 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.00 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.00 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.00 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.00 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.10 0.17 8.79 0.0 2.49 0.00 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ...
436 42101000200 -75.1631 39.9529 -75.1511 39.9578 2 Philadelphia County, Pennsylvania 1362.00 56.42 374.0 81.48 421.0 8349.0 55600.0 31.2 11.16 5.21 1.69 0.07 79.59 0.07 2.20 0.00 NaN NaN NaN NaN 0.0 0.0 0.0 1362.00 56.42 415.0 81.48 421.0 8349.0 55600.0 31.2 11.16 5.21 1.69 0.07 79.59 0.07 2.20 0.00 NaN NaN NaN NaN 0.0 0.0 0.0 1362.00 56.42 455.0 81.48 421.0 8349.0 55600.0 31.2 11.16 5.21 1.69 0.07 79.59 0.07 2.20 0.00 4.0 4.0 0.88 0.88 1.0 0.0 0.0 1362.00 56.42 496.0 81.48 421.0 8349.0 55600.0 31.2 11.16 5.21 1.69 0.07 79.59 0.07 2.20 0.00 3.0 3.0 0.60 0.60 1.0 0.0 0.0 1362.00 56.42 537.0 81.48 421.0 8349.0 55600.0 31.2 11.16 5.21 1.69 0.07 79.59 0.07 2.20 0.00 6.0 6.0 1.12 1.12 1.0 0.0 0.0 1633.00 3.45 578.0 56.04 675.0 42083.0 232800.0 24.7 19.29 2.82 1.22 0.0 76.00 0.0 0.67 0.00 1.0 0.0 0.00 0.17 1.0 0.0 0.0 1633.00 3.45 618.0 56.04 675.0 42083.0 232800.0 24.7 19.29 2.82 1.22 0.0 76.00 0.0 0.67 0.00 6.0 6.0 0.97 0.97 1.0 0.0 0.0 1633.00 3.45 659.0 56.04 675.0 42083.0 232800.0 24.7 19.29 2.82 1.22 0.0 76.00 0.0 0.67 0.00 9.0 7.0 1.06 1.37 0.0 0.0 1.0 1633.00 3.45 700.0 56.04 675.0 42083.0 232800.0 24.7 19.29 2.82 1.22 0.0 76.00 0.0 0.67 0.00 11.0 7.0 1.00 1.57 0.0 0.0 1.0 1633.00 3.45 740.0 56.04 675.0 42083.0 232800.0 24.7 19.29 2.82 1.22 0.0 76.00 0.0 0.67 0.00 6.0 5.0 0.68 0.81 0.0 0.0 1.0 2937.0 5.07 781.0 68.21 905.0 49928.0 261100.0 26.4 22.64 9.67 2.69 0.10 63.16 0.03 1.40 0.31 6.0 1.0 0.13 0.77 0.0 0.0 1.0 2331.0 15.78 792.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 9.0 6.0 0.76 1.14 0.0 0.0 1.0 2331.0 15.78 802.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 8.0 3.0 0.37 1.00 0.0 0.0 1.0 2331.0 15.78 813.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 14.0 10.0 1.23 1.72 0.0 0.0 1.0 2331.0 15.78 824.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 5.0 3.0 0.36 0.61 0.0 0.0 1.0 2331.0 15.78 834.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 10.0 9.0 1.08 1.20 0.0 0.0 1.0 2331.0 15.78 845.0 60.27 1263.0 59891.0 265400.0 28.1 38.91 6.05 1.54 0.47 50.75 0.0 2.27 0.00 11.0 8.0 0.95 1.30 0.0 0.0 1.0 MULTIPOLYGON (((-75.15122 39.95686, -75.15167 ...
437 42101000300 -75.1798 39.9544 -75.1623 39.9599 3 Philadelphia County, Pennsylvania 2570.00 12.16 861.0 69.49 688.0 40625.0 233900.0 29.0 70.86 14.67 3.81 0.27 7.00 0.08 3.04 0.27 NaN NaN NaN NaN 0.0 0.0 0.0 2570.00 12.16 915.0 69.49 688.0 40625.0 233900.0 29.0 70.86 14.67 3.81 0.27 7.00 0.08 3.04 0.27 NaN NaN NaN NaN 0.0 0.0 0.0 2570.00 12.16 969.0 69.49 688.0 40625.0 233900.0 29.0 70.86 14.67 3.81 0.27 7.00 0.08 3.04 0.27 14.0 12.0 1.24 1.44 1.0 0.0 0.0 2570.00 12.16 1023.0 69.49 688.0 40625.0 233900.0 29.0 70.86 14.67 3.81 0.27 7.00 0.08 3.04 0.27 21.0 17.0 1.66 2.05 1.0 0.0 0.0 2570.00 12.16 1077.0 69.49 688.0 40625.0 233900.0 29.0 70.86 14.67 3.81 0.27 7.00 0.08 3.04 0.27 23.0 23.0 2.13 2.13 1.0 0.0 0.0 4497.00 1.63 1132.0 65.66 1184.0 59189.0 438500.0 24.8 67.44 10.52 5.69 0.2 14.14 0.0 1.29 0.71 12.0 10.0 0.88 1.06 1.0 0.0 0.0 4497.00 1.63 1186.0 65.66 1184.0 59189.0 438500.0 24.8 67.44 10.52 5.69 0.2 14.14 0.0 1.29 0.71 19.0 16.0 1.35 1.60 1.0 0.0 0.0 4497.00 1.63 1240.0 65.66 1184.0 59189.0 438500.0 24.8 67.44 10.52 5.69 0.2 14.14 0.0 1.29 0.71 21.0 7.0 0.56 1.69 0.0 0.0 1.0 4497.00 1.63 1294.0 65.66 1184.0 59189.0 438500.0 24.8 67.44 10.52 5.69 0.2 14.14 0.0 1.29 0.71 25.0 11.0 0.85 1.93 0.0 0.0 1.0 4497.00 1.63 1348.0 65.66 1184.0 59189.0 438500.0 24.8 67.44 10.52 5.69 0.2 14.14 0.0 1.29 0.71 27.0 12.0 0.89 2.00 0.0 0.0 1.0 3169.0 7.20 1402.0 75.58 1827.0 71250.0 451800.0 28.0 72.26 10.22 4.26 0.03 10.35 0.03 2.52 0.32 24.0 13.0 0.93 1.71 0.0 0.0 1.0 3405.0 4.17 1489.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 21.0 8.0 0.54 1.41 0.0 0.0 1.0 3405.0 4.17 1575.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 27.0 12.0 0.76 1.71 0.0 0.0 1.0 3405.0 4.17 1662.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 31.0 10.0 0.60 1.87 0.0 0.0 1.0 3405.0 4.17 1749.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 27.0 14.0 0.80 1.54 0.0 0.0 1.0 3405.0 4.17 1835.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 18.0 5.0 0.27 0.98 0.0 0.0 1.0 3405.0 4.17 1922.0 70.47 1938.0 81950.0 469900.0 26.2 72.19 5.26 8.75 0.00 12.04 0.0 1.76 0.00 26.0 14.0 0.73 1.35 0.0 0.0 1.0 MULTIPOLYGON (((-75.16234 39.95782, -75.16237 ...
438 42101000801 -75.1833 39.9486 -75.1773 39.9515 8.01 Philadelphia County, Pennsylvania 1478.00 14.40 810.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 NaN NaN NaN NaN 0.0 0.0 0.0 1478.00 14.40 801.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 NaN NaN NaN NaN 0.0 0.0 0.0 1478.00 14.40 793.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 7.0 5.0 0.63 0.88 1.0 0.0 0.0 1478.00 14.40 784.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 19.0 13.0 1.66 2.42 1.0 0.0 0.0 1478.00 14.40 775.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 17.0 14.0 1.81 2.19 1.0 0.0 0.0 1344.37 11.10 767.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 10.0 6.0 0.78 1.30 1.0 0.0 0.0 1344.37 11.10 758.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 12.0 7.0 0.92 1.58 1.0 0.0 0.0 1344.37 11.10 749.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 12.0 5.0 0.67 1.60 0.0 0.0 1.0 1344.37 11.10 740.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 11.0 4.0 0.54 1.49 0.0 0.0 1.0 1344.37 11.10 732.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 10.0 2.0 0.27 1.37 0.0 0.0 1.0 1562.0 2.46 723.0 71.09 2001.0 83125.0 459900.0 25.9 78.04 2.94 5.76 0.00 10.82 0.26 1.92 0.26 14.0 4.0 0.55 1.94 0.0 0.0 1.0 1692.0 3.25 734.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 13.0 7.0 0.95 1.77 0.0 0.0 1.0 1692.0 3.25 746.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 7.0 0.0 0.00 0.94 0.0 0.0 1.0 1692.0 3.25 757.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 15.0 3.0 0.40 1.98 0.0 0.0 1.0 1692.0 3.25 768.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 10.0 4.0 0.52 1.30 0.0 0.0 1.0 1692.0 3.25 780.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 16.0 8.0 1.03 2.05 0.0 0.0 1.0 1692.0 3.25 791.0 74.87 2219.0 81620.0 656300.0 24.7 75.18 10.64 4.91 0.00 4.08 0.0 1.42 3.78 13.0 4.0 0.51 1.64 0.0 0.0 1.0 MULTIPOLYGON (((-75.17732 39.95096, -75.17784 ...
439 42101000804 -75.1712 39.9470 -75.1643 39.9501 8.04 Philadelphia County, Pennsylvania 3301.00 14.40 2058.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 NaN NaN NaN NaN 0.0 0.0 0.0 3301.00 14.40 2050.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 NaN NaN NaN NaN 0.0 0.0 0.0 3301.00 14.40 2042.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 22.0 18.0 0.88 1.08 1.0 0.0 0.0 3301.00 14.40 2033.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 31.0 21.0 1.03 1.52 1.0 0.0 0.0 3301.00 14.40 2025.0 73.65 933.0 42346.0 265200.0 27.6 81.67 2.97 3.50 0.04 10.08 0.04 1.26 0.45 18.0 15.0 0.74 0.89 1.0 0.0 0.0 3002.54 11.10 2017.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 28.0 19.0 0.94 1.39 1.0 0.0 0.0 3002.54 11.10 2009.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 14.0 13.0 0.65 0.70 1.0 0.0 0.0 3002.54 11.10 2001.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 33.0 11.0 0.55 1.65 0.0 0.0 1.0 3002.54 11.10 1992.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 17.0 4.0 0.20 0.85 0.0 0.0 1.0 3002.54 11.10 1984.0 66.11 1393.0 61590.0 431800.0 28.2 86.58 1.05 3.14 0.0 8.16 0.0 0.88 0.18 27.0 8.0 0.40 1.36 0.0 0.0 1.0 3609.0 7.69 1976.0 76.32 1562.0 75357.0 330200.0 26.0 78.55 2.72 4.96 0.03 11.75 0.03 1.72 0.25 43.0 13.0 0.66 2.18 0.0 0.0 1.0 3746.0 0.00 2000.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 38.0 9.0 0.45 1.90 0.0 0.0 1.0 3746.0 0.00 2024.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 31.0 16.0 0.79 1.53 0.0 0.0 1.0 3746.0 0.00 2048.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 27.0 8.0 0.39 1.32 0.0 0.0 1.0 3746.0 0.00 2072.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 28.0 11.0 0.53 1.35 0.0 0.0 1.0 3746.0 0.00 2096.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 18.0 7.0 0.33 0.86 0.0 0.0 1.0 3746.0 0.00 2120.0 75.43 1816.0 96250.0 465500.0 23.7 67.43 4.51 13.59 0.35 13.59 0.0 0.19 0.35 22.0 7.0 0.33 1.04 0.0 0.0 1.0 MULTIPOLYGON (((-75.17118 39.94778, -75.17102 ...

1.1.3 Transform from wide to tidy format

For this assignment, we are interested in the number of evictions by census tract for various years. Right now, each year has it’s own column, so it will be easiest to transform to a tidy format.

Use the pd.melt() function to transform the eviction data into tidy format, using the number of evictions from 2003 to 2016.

The tidy data frame should have four columns: GEOID, geometry, a column holding the number of evictions, and a column telling you what the name of the original column was for that value.

Hints: - You’ll want to specify the GEOID and geometry columns as the id_vars. This will keep track of the census tract information. - You should specify the names of the columns holding the number of evictions as the value_vars. - You can generate a list of this column names using Python’s f-string formatting: python value_vars = [f"e-{x:02d}" for x in range(3, 17)]

# Generate the list of eviction columns for years 2003 to 2016
value_vars = [f"e-{x:02d}" for x in range(3, 17)]
philly_evictions = pd.melt(
    philly_tracts,
    id_vars=['GEOID', 'geometry'],  
    value_vars=value_vars,  
    var_name='year',  
    value_name='evictions' 
)

philly_evictions.head()
GEOID geometry year evictions
0 42101000100 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... e-03 21.0
1 42101000200 MULTIPOLYGON (((-75.15122 39.95686, -75.15167 ... e-03 3.0
2 42101000300 MULTIPOLYGON (((-75.16234 39.95782, -75.16237 ... e-03 17.0
3 42101000801 MULTIPOLYGON (((-75.17732 39.95096, -75.17784 ... e-03 13.0
4 42101000804 MULTIPOLYGON (((-75.17118 39.94778, -75.17102 ... e-03 21.0

1.1.4 Plot the total number of evictions per year from 2003 to 2016

Use hvplot to plot the total number of evictions from 2003 to 2016. You will first need to perform a group by operation and sum up the total number of evictions for all census tracts, and then use hvplot() to make your plot.

You can use any type of hvplot chart you’d like to show the trend in number of evictions over time.

import holoviews as hv
import hvplot.pandas
# Group by year and sum the evictions for all census tracts
total_evictions_per_year = philly_evictions.groupby('year')['evictions'].sum().reset_index()
# Plot the total number of evictions per year using hvplot
eviction_plot = total_evictions_per_year.hvplot(
    x='year',
    y='evictions',
    kind='line',
    title='Total Number of Evictions in Philadelphia (2003-2016)',
    xlabel='Year',
    ylabel='Total Number of Evictions',
    line_width=2,
    grid=True,
    width=600, height=400
)

eviction_plot

1.1.5 The number of evictions across Philadelphia

Our tidy data frame is still a GeoDataFrame with a geometry column, so we can visualize the number of evictions for all census tracts.

Use hvplot() to generate a choropleth showing the number of evictions for a specified year, with a widget dropdown to select a given year (or variable name, e.g., e-16, e-15, etc).

Hints - You’ll need to use the groupby keyword to tell hvplot to make a series of maps, with a widget to select between them. - You will need to specify dynamic=False as a keyword argument to the hvplot() function. - Be sure to specify a width and height that makes your output map (roughly) square to limit distortions

choropleth = philly_evictions.hvplot(
    geo=True,  
    c='evictions',  
    frame_width=600,  
    frame_height=600,  
    cmap='viridis',  
    hover_cols=['GEOID'], 
    groupby='year',  # Group by the 'year' column to create an interactive dropdown for different years
    title='Number of Evictions in Philadelphia Census Tracts by Year',
    dynamic=False 
)

choropleth

1.2 Code Violations in Philadelphia

Next, we’ll explore data for code violations from the Licenses and Inspections Department of Philadelphia to look for potential correlations with the number of evictions.

1.2.1 Load data from 2012 to 2016

L+I violation data for years including 2012 through 2016 (inclusive) is provided in a CSV format in the “data/” folder.

Load the data using pandas and convert to a GeoDataFrame.

li_violations = pd.read_csv("data/li_violations.csv")
li_violations.head()
lat lng violationdescription
0 40.050526 -75.126076 CLIP VIOLATION NOTICE
1 40.050593 -75.126578 LICENSE-CHANGE OF ADDRESS
2 40.050593 -75.126578 LICENSE-RES SFD/2FD
3 39.991994 -75.128895 EXT A-CLEAN WEEDS/PLANTS
4 40.023260 -75.164848 EXT A-VACANT LOT CLEAN/MAINTAI
# Use the helper utility function: geopandas.points_from_xy() to create Point objects for each lat and lon combination.

li_violations["geometry"] = gpd.points_from_xy(
    li_violations["lng"], li_violations["lat"]
)
li_violations["geometry"].head()
0    POINT (-75.12608 40.05053)
1    POINT (-75.12658 40.05059)
2    POINT (-75.12658 40.05059)
3    POINT (-75.12889 39.99199)
4    POINT (-75.16485 40.02326)
Name: geometry, dtype: geometry
# Create GeoDataFrame
li_violations_gdf = gpd.GeoDataFrame(
    li_violations, geometry="geometry", crs="EPSG:4326"
)

1.2.2 Trim to specific violation types

There are many different types of code violations (running the nunique() function on the violationdescription column will extract all of the unique ones). More information on different types of violations can be found on the City’s website.

Below, I’ve selected 15 types of violations that deal with property maintenance and licensing issues. We’ll focus on these violations. The goal is to see if these kinds of violations are correlated spatially with the number of evictions in a given area.

Use the list of violations given to trim your data set to only include these types.

# View the number of different types of violations
unique_violation_types = li_violations_gdf['violationdescription'].nunique()
print(f"Number of unique violation types: {unique_violation_types}")
Number of unique violation types: 1342
violation_types = [
    "INT-PLMBG MAINT FIXTURES-RES",
    "INT S-CEILING REPAIR/MAINT SAN",
    "PLUMBING SYSTEMS-GENERAL",
    "CO DETECTOR NEEDED",
    "INTERIOR SURFACES",
    "EXT S-ROOF REPAIR",
    "ELEC-RECEPTABLE DEFECTIVE-RES",
    "INT S-FLOOR REPAIR",
    "DRAINAGE-MAIN DRAIN REPAIR-RES",
    "DRAINAGE-DOWNSPOUT REPR/REPLC",
    "LIGHT FIXTURE DEFECTIVE-RES",
    "LICENSE-RES SFD/2FD",
    "ELECTRICAL -HAZARD",
    "VACANT PROPERTIES-GENERAL",
    "INT-PLMBG FIXTURES-RES",
]
# Filter the GeoDataFrame to only include rows with the specified violation types
filtered_violations_gdf = li_violations_gdf[li_violations_gdf['violationdescription'].isin(violation_types)]

# Check the filtered GeoDataFrame
filtered_violations_gdf.head()
lat lng violationdescription geometry
2 40.050593 -75.126578 LICENSE-RES SFD/2FD POINT (-75.12658 40.05059)
25 40.022406 -75.121872 EXT S-ROOF REPAIR POINT (-75.12187 40.02241)
30 40.023237 -75.121726 CO DETECTOR NEEDED POINT (-75.12173 40.02324)
31 40.023397 -75.122241 INT S-CEILING REPAIR/MAINT SAN POINT (-75.12224 40.02340)
34 40.023773 -75.121603 INT S-FLOOR REPAIR POINT (-75.12160 40.02377)

1.2.3 Make a hex bin map

The code violation data is point data. We can get a quick look at the geographic distribution using matplotlib and the hexbin() function. Make a hex bin map of the code violations and overlay the census tract outlines.

Hints: - The eviction data from part 1 was by census tract, so the census tract geometries are available as part of that GeoDataFrame. You can use it to overlay the census tracts on your hex bin map. - Make sure you convert your GeoDataFrame to a CRS that’s better for visualization than plain old 4326.

# Convert GeoDataFrame to a projection suitable for visualization
violations_3857 = filtered_violations_gdf.to_crs(epsg=3857)
# Create the axes
fig, ax = plt.subplots(figsize=(10, 8))

# Extract out the x/y coordindates of the Point objects
xcoords = violations_3857.geometry.x
ycoords = violations_3857.geometry.y

# Plot a hexbin chart
hex_violations = ax.hexbin(xcoords, ycoords, gridsize=50)

# Add the geometry boundaries
philly_tracts.to_crs(violations_3857.crs).plot(
    ax=ax, facecolor="none", edgecolor="white", linewidth=0.25
)

# Add a colorbar and format
fig.colorbar(hex_vals, ax=ax)
ax.set_axis_off()
ax.set_aspect("equal")

1.2.4 Spatially join data sets

To do a census tract comparison to our eviction data, we need to find which census tract each of the code violations falls into. Use the geopandas.sjoin() function to do just that.

Hints - You can re-use your eviction data frame, but you will only need the geometry column (specifying census tract polygons) and the GEOID column (specifying the name of each census tract). - Make sure both data frames have the same CRS before joining them together!

# Spatially join the filtered violations with the census tracts to identify which tract each violation falls into
violations_with_tracts = gpd.sjoin(
    filtered_violations_gdf,  # The data for violations
    philly_tracts[['geometry', 'GEOID']].to_crs(filtered_violations_gdf.crs),  # The census tracts (in the same CRS)
    predicate="within",
    how="left",
)

violations_with_tracts.head()
lat lng violationdescription geometry index_right GEOID
2 40.050593 -75.126578 LICENSE-RES SFD/2FD POINT (-75.12658 40.05059) 3185 42101027100
25 40.022406 -75.121872 EXT S-ROOF REPAIR POINT (-75.12187 40.02241) 2238 42101028800
30 40.023237 -75.121726 CO DETECTOR NEEDED POINT (-75.12173 40.02324) 2238 42101028800
31 40.023397 -75.122241 INT S-CEILING REPAIR/MAINT SAN POINT (-75.12224 40.02340) 2238 42101028800
34 40.023773 -75.121603 INT S-FLOOR REPAIR POINT (-75.12160 40.02377) 2238 42101028800

1.2.5 Calculate the number of violations by type per census tract

Next, we’ll want to find the number of violations (for each kind) per census tract. You should group the data frame by violation type and census tract name.

The result of this step should be a data frame with three columns: violationdescription, GEOID, and N, where N is the number of violations of that kind in the specified census tract.

Optional: to make prettier plots

Some census tracts won’t have any violations, and they won’t be included when we do the above calculation. However, there is a trick to set the values for those census tracts to be zero. After you calculate the sizes of each violation/census tract group, you can run:

N = N.unstack(fill_value=0).stack().reset_index(name='N')

where N gives the total size of each of the groups, specified by violation type and census tract name.

See this StackOverflow post for more details.

This part is optional, but will make the resulting maps a bit prettier.

# Calculate the number of violations by type per census tract
violation_counts = violations_with_tracts.groupby(['violationdescription', 'GEOID']).size().reset_index(name='N')

type(violation_counts)
pandas.core.frame.DataFrame
# Fill in missing census tracts with zero violations
violation_counts = violation_counts.set_index(['violationdescription', 'GEOID']).unstack(fill_value=0).stack().reset_index().rename(columns={0: 'N'})

violation_counts.head()
violationdescription GEOID N
0 CO DETECTOR NEEDED 42101000100 0
1 CO DETECTOR NEEDED 42101000200 0
2 CO DETECTOR NEEDED 42101000300 0
3 CO DETECTOR NEEDED 42101000401 1
4 CO DETECTOR NEEDED 42101000402 1

1.2.6 Merge with census tracts geometries

We now have the number of violations of different types per census tract specified as a regular DataFrame. You can now merge it with the census tract geometries (from your eviction data GeoDataFrame) to create a GeoDataFrame.

Hints - Use pandas.merge() and specify the on keyword to be the column holding census tract names. - Make sure the result of the merge operation is a GeoDataFrame — you will want the GeoDataFrame holding census tract geometries to be the first argument of the pandas.merge() function.

# Merge the violation counts with the census tract geometries to create a GeoDataFrame
violations_by_tracts = philly_tracts.merge(violation_counts, on="GEOID")

violations_by_tracts['N'] = violations_by_tracts['N'].fillna(0)

violations_by_tracts.head()
GEOID west south east north n pl p-00 pr-00 roh-00 pro-00 mgr-00 mhi-00 mpv-00 rb-00 pw-00 paa-00 ph-00 pai-00 pa-00 pnp-00 pm-00 po-00 ef-00 e-00 er-00 efr-00 lf-00 imputed-00 subbed-00 p-01 pr-01 roh-01 pro-01 mgr-01 mhi-01 mpv-01 rb-01 pw-01 paa-01 ph-01 pai-01 pa-01 pnp-01 pm-01 po-01 ef-01 e-01 er-01 efr-01 lf-01 imputed-01 subbed-01 p-02 pr-02 roh-02 pro-02 mgr-02 mhi-02 mpv-02 rb-02 pw-02 paa-02 ph-02 pai-02 pa-02 pnp-02 pm-02 po-02 ef-02 e-02 er-02 efr-02 lf-02 imputed-02 subbed-02 p-03 pr-03 roh-03 pro-03 mgr-03 mhi-03 mpv-03 rb-03 pw-03 paa-03 ph-03 pai-03 pa-03 pnp-03 pm-03 po-03 ef-03 e-03 er-03 efr-03 lf-03 imputed-03 subbed-03 p-04 pr-04 roh-04 pro-04 mgr-04 mhi-04 mpv-04 rb-04 pw-04 paa-04 ph-04 pai-04 pa-04 pnp-04 pm-04 po-04 ef-04 e-04 er-04 efr-04 lf-04 imputed-04 subbed-04 p-05 pr-05 roh-05 pro-05 mgr-05 mhi-05 mpv-05 rb-05 pw-05 paa-05 ph-05 pai-05 pa-05 pnp-05 pm-05 po-05 ef-05 e-05 er-05 efr-05 lf-05 imputed-05 subbed-05 p-06 pr-06 roh-06 pro-06 mgr-06 mhi-06 mpv-06 rb-06 pw-06 paa-06 ph-06 pai-06 pa-06 pnp-06 pm-06 po-06 ef-06 e-06 er-06 efr-06 lf-06 imputed-06 subbed-06 p-07 pr-07 roh-07 pro-07 mgr-07 mhi-07 mpv-07 rb-07 pw-07 paa-07 ph-07 pai-07 pa-07 pnp-07 pm-07 po-07 ef-07 e-07 er-07 efr-07 lf-07 imputed-07 subbed-07 p-08 pr-08 roh-08 pro-08 mgr-08 mhi-08 mpv-08 rb-08 pw-08 paa-08 ph-08 pai-08 pa-08 pnp-08 pm-08 po-08 ef-08 e-08 er-08 efr-08 lf-08 imputed-08 subbed-08 p-09 pr-09 roh-09 pro-09 mgr-09 mhi-09 mpv-09 rb-09 pw-09 paa-09 ph-09 pai-09 pa-09 pnp-09 pm-09 po-09 ef-09 e-09 er-09 efr-09 lf-09 imputed-09 subbed-09 p-10 pr-10 roh-10 pro-10 mgr-10 mhi-10 mpv-10 rb-10 pw-10 paa-10 ph-10 pai-10 pa-10 pnp-10 pm-10 po-10 ef-10 e-10 er-10 efr-10 lf-10 imputed-10 subbed-10 p-11 pr-11 roh-11 pro-11 mgr-11 mhi-11 mpv-11 rb-11 pw-11 paa-11 ph-11 pai-11 pa-11 pnp-11 pm-11 po-11 ef-11 e-11 er-11 efr-11 lf-11 imputed-11 subbed-11 p-12 pr-12 roh-12 pro-12 mgr-12 mhi-12 mpv-12 rb-12 pw-12 paa-12 ph-12 pai-12 pa-12 pnp-12 pm-12 po-12 ef-12 e-12 er-12 efr-12 lf-12 imputed-12 subbed-12 p-13 pr-13 roh-13 pro-13 mgr-13 mhi-13 mpv-13 rb-13 pw-13 paa-13 ph-13 pai-13 pa-13 pnp-13 pm-13 po-13 ef-13 e-13 er-13 efr-13 lf-13 imputed-13 subbed-13 p-14 pr-14 roh-14 pro-14 mgr-14 mhi-14 mpv-14 rb-14 pw-14 paa-14 ph-14 pai-14 pa-14 pnp-14 pm-14 po-14 ef-14 e-14 er-14 efr-14 lf-14 imputed-14 subbed-14 p-15 pr-15 roh-15 pro-15 mgr-15 mhi-15 mpv-15 rb-15 pw-15 paa-15 ph-15 pai-15 pa-15 pnp-15 pm-15 po-15 ef-15 e-15 er-15 efr-15 lf-15 imputed-15 subbed-15 p-16 pr-16 roh-16 pro-16 mgr-16 mhi-16 mpv-16 rb-16 pw-16 paa-16 ph-16 pai-16 pa-16 pnp-16 pm-16 po-16 ef-16 e-16 er-16 efr-16 lf-16 imputed-16 subbed-16 geometry violationdescription N
0 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... CO DETECTOR NEEDED 0
1 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... DRAINAGE-DOWNSPOUT REPR/REPLC 6
2 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... DRAINAGE-MAIN DRAIN REPAIR-RES 0
3 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... ELEC-RECEPTABLE DEFECTIVE-RES 0
4 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... ELECTRICAL -HAZARD 1

1.2.7 Interactive choropleths for each violation type

Now, we can use hvplot() to create an interactive choropleth for each violation type and add a widget to specify different violation types.

Hints - You’ll need to use the groupby keyword to tell hvplot to make a series of maps, with a widget to select different violation types. - You will need to specify dynamic=False as a keyword argument to the hvplot() function. - Be sure to specify a width and height that makes your output map (roughly) square to limit distortions

# Create an interactive choropleth for each violation type using hvplot
choropleth_2 = violations_by_tracts.hvplot.polygons(
    geo=True,
    tiles='CartoLight',
    color='N',
    groupby='violationdescription',
    dynamic=False,
    frame_width=600,
    frame_height=600,
    cmap='Viridis',
    hover_cols=['GEOID', 'N']
)

# Display the interactive choropleth
choropleth_2

1.3. A side-by-side comparison

From the interactive maps of evictions and violations, you should notice a lot of spatial overlap.

As a final step, we’ll make a side-by-side comparison to better show the spatial correlations. This will involve a few steps:

  1. Trim the evictions data frame plotted in section 1.1.5 to only include evictions from 2016.
  2. Trim the L+I violations data frame plotted in section 1.2.7 to only include a single violation type (pick whichever one you want!).
  3. Use hvplot() to make two interactive choropleth maps, one for the data from step 1. and one for the data in step 2.
  4. Show these two plots side by side (one row and 2 columns) using the syntax for combining charts.

Note: since we selected a single year and violation type, you won’t need to use the groupby= keyword here.

# Trim the evictions data to only include evictions from 2016
evictions_2016 = philly_evictions[philly_evictions['year'] == 'e-16'][['GEOID', 'geometry', 'evictions']].copy()
# Trim the L+I violations data to only include a single violation type
selected_violation = violations_by_tracts[violations_by_tracts['violationdescription'] == 'CO DETECTOR NEEDED']
# Create an interactive choropleth for evictions in 2016 using hvplot
choropleth_evictions = evictions_2016.hvplot.polygons(
    geo=True,
    tiles='CartoLight',
    color='evictions_2016',
    frame_width=600,
    frame_height=600,
    cmap='Viridis',
    hover_cols=['GEOID']
)

# Create an interactive choropleth for the selected violation type using hvplot
choropleth_violation = selected_violation.hvplot.polygons(
    geo=True,
    tiles='CartoLight',
    color='N',
    frame_width=600,
    frame_height=600,
    cmap='Viridis',
    hover_cols=['GEOID', 'N']
)

# Display the two choropleths side by side
comparison = (choropleth_evictions + choropleth_violation).cols(2)

# Display the side-by-side comparison
comparison

1.4. Extra Credit

Identify the 20 most common types of violations within the time period of 2012 to 2016 and create a set of interactive choropleths similar to what was done in section 1.2.7.

Use this set of maps to identify 3 types of violations that don’t seem to have much spatial overlap with the number of evictions in the City.

# Identify the 20 most common types of violations from 2012 to 2016
most_common_violations = li_violations['violationdescription'].value_counts().head(20).index.tolist()

# Filter the GeoDataFrame to only include rows with the 20 most common violation types
violations_gdf_20 = li_violations_gdf[li_violations_gdf['violationdescription'].isin(most_common_violations)]

violations_gdf_20.head()
lat lng violationdescription geometry
0 40.050526 -75.126076 CLIP VIOLATION NOTICE POINT (-75.12608 40.05053)
2 40.050593 -75.126578 LICENSE-RES SFD/2FD POINT (-75.12658 40.05059)
3 39.991994 -75.128895 EXT A-CLEAN WEEDS/PLANTS POINT (-75.12889 39.99199)
4 40.023260 -75.164848 EXT A-VACANT LOT CLEAN/MAINTAI POINT (-75.16485 40.02326)
5 40.023260 -75.164848 EXT A-VACANT LOT CLEAN/MAINTAI POINT (-75.16485 40.02326)
# Spatially join the filtered violations with the census tracts to identify which tract each violation falls into
violations_with_tracts_20 = gpd.sjoin(
    violations_gdf_20,  # The point data for violations
    philly_tracts[['geometry', 'GEOID']].to_crs(filtered_violations_gdf.crs),  # The neighborhoods (in the same CRS)
    predicate="within",
    how="left",
)

violations_with_tracts_20.head()
lat lng violationdescription geometry index_right GEOID
0 40.050526 -75.126076 CLIP VIOLATION NOTICE POINT (-75.12608 40.05053) 3185 42101027100
2 40.050593 -75.126578 LICENSE-RES SFD/2FD POINT (-75.12658 40.05059) 3185 42101027100
3 39.991994 -75.128895 EXT A-CLEAN WEEDS/PLANTS POINT (-75.12889 39.99199) 525 42101017601
4 40.023260 -75.164848 EXT A-VACANT LOT CLEAN/MAINTAI POINT (-75.16485 40.02326) 3102 42101024400
5 40.023260 -75.164848 EXT A-VACANT LOT CLEAN/MAINTAI POINT (-75.16485 40.02326) 3102 42101024400
# Calculate the number of violations by type per census tract
violations_counts_20 = violations_with_tracts_20.groupby(['violationdescription', 'GEOID']).size().reset_index(name='N')

# Fill in missing census tracts with zero violations
violations_counts_20 = violations_counts_20.set_index(['violationdescription', 'GEOID']).unstack(fill_value=0).stack().reset_index().rename(columns={0: 'N'})

violations_counts_20.head()
violationdescription GEOID N
0 ANNUAL CERT FIRE ALARM 42101000100 55
1 ANNUAL CERT FIRE ALARM 42101000200 38
2 ANNUAL CERT FIRE ALARM 42101000300 27
3 ANNUAL CERT FIRE ALARM 42101000401 12
4 ANNUAL CERT FIRE ALARM 42101000402 33
# Merge the violation counts with the census tract geometries to create a GeoDataFrame
violations_by_tracts_20 = philly_tracts.merge(violations_counts_20, on="GEOID")

violations_by_tracts_20.head()
GEOID west south east north n pl p-00 pr-00 roh-00 pro-00 mgr-00 mhi-00 mpv-00 rb-00 pw-00 paa-00 ph-00 pai-00 pa-00 pnp-00 pm-00 po-00 ef-00 e-00 er-00 efr-00 lf-00 imputed-00 subbed-00 p-01 pr-01 roh-01 pro-01 mgr-01 mhi-01 mpv-01 rb-01 pw-01 paa-01 ph-01 pai-01 pa-01 pnp-01 pm-01 po-01 ef-01 e-01 er-01 efr-01 lf-01 imputed-01 subbed-01 p-02 pr-02 roh-02 pro-02 mgr-02 mhi-02 mpv-02 rb-02 pw-02 paa-02 ph-02 pai-02 pa-02 pnp-02 pm-02 po-02 ef-02 e-02 er-02 efr-02 lf-02 imputed-02 subbed-02 p-03 pr-03 roh-03 pro-03 mgr-03 mhi-03 mpv-03 rb-03 pw-03 paa-03 ph-03 pai-03 pa-03 pnp-03 pm-03 po-03 ef-03 e-03 er-03 efr-03 lf-03 imputed-03 subbed-03 p-04 pr-04 roh-04 pro-04 mgr-04 mhi-04 mpv-04 rb-04 pw-04 paa-04 ph-04 pai-04 pa-04 pnp-04 pm-04 po-04 ef-04 e-04 er-04 efr-04 lf-04 imputed-04 subbed-04 p-05 pr-05 roh-05 pro-05 mgr-05 mhi-05 mpv-05 rb-05 pw-05 paa-05 ph-05 pai-05 pa-05 pnp-05 pm-05 po-05 ef-05 e-05 er-05 efr-05 lf-05 imputed-05 subbed-05 p-06 pr-06 roh-06 pro-06 mgr-06 mhi-06 mpv-06 rb-06 pw-06 paa-06 ph-06 pai-06 pa-06 pnp-06 pm-06 po-06 ef-06 e-06 er-06 efr-06 lf-06 imputed-06 subbed-06 p-07 pr-07 roh-07 pro-07 mgr-07 mhi-07 mpv-07 rb-07 pw-07 paa-07 ph-07 pai-07 pa-07 pnp-07 pm-07 po-07 ef-07 e-07 er-07 efr-07 lf-07 imputed-07 subbed-07 p-08 pr-08 roh-08 pro-08 mgr-08 mhi-08 mpv-08 rb-08 pw-08 paa-08 ph-08 pai-08 pa-08 pnp-08 pm-08 po-08 ef-08 e-08 er-08 efr-08 lf-08 imputed-08 subbed-08 p-09 pr-09 roh-09 pro-09 mgr-09 mhi-09 mpv-09 rb-09 pw-09 paa-09 ph-09 pai-09 pa-09 pnp-09 pm-09 po-09 ef-09 e-09 er-09 efr-09 lf-09 imputed-09 subbed-09 p-10 pr-10 roh-10 pro-10 mgr-10 mhi-10 mpv-10 rb-10 pw-10 paa-10 ph-10 pai-10 pa-10 pnp-10 pm-10 po-10 ef-10 e-10 er-10 efr-10 lf-10 imputed-10 subbed-10 p-11 pr-11 roh-11 pro-11 mgr-11 mhi-11 mpv-11 rb-11 pw-11 paa-11 ph-11 pai-11 pa-11 pnp-11 pm-11 po-11 ef-11 e-11 er-11 efr-11 lf-11 imputed-11 subbed-11 p-12 pr-12 roh-12 pro-12 mgr-12 mhi-12 mpv-12 rb-12 pw-12 paa-12 ph-12 pai-12 pa-12 pnp-12 pm-12 po-12 ef-12 e-12 er-12 efr-12 lf-12 imputed-12 subbed-12 p-13 pr-13 roh-13 pro-13 mgr-13 mhi-13 mpv-13 rb-13 pw-13 paa-13 ph-13 pai-13 pa-13 pnp-13 pm-13 po-13 ef-13 e-13 er-13 efr-13 lf-13 imputed-13 subbed-13 p-14 pr-14 roh-14 pro-14 mgr-14 mhi-14 mpv-14 rb-14 pw-14 paa-14 ph-14 pai-14 pa-14 pnp-14 pm-14 po-14 ef-14 e-14 er-14 efr-14 lf-14 imputed-14 subbed-14 p-15 pr-15 roh-15 pro-15 mgr-15 mhi-15 mpv-15 rb-15 pw-15 paa-15 ph-15 pai-15 pa-15 pnp-15 pm-15 po-15 ef-15 e-15 er-15 efr-15 lf-15 imputed-15 subbed-15 p-16 pr-16 roh-16 pro-16 mgr-16 mhi-16 mpv-16 rb-16 pw-16 paa-16 ph-16 pai-16 pa-16 pnp-16 pm-16 po-16 ef-16 e-16 er-16 efr-16 lf-16 imputed-16 subbed-16 geometry violationdescription N
0 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... ANNUAL CERT FIRE ALARM 55
1 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... CLIP VIOLATION NOTICE 5
2 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... CO DETECTOR NEEDED 0
3 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... EXT A-CLEAN RUBBISH/GARBAGE 4
4 42101000100 -75.1523 39.9481 -75.1415 39.9569 1 Philadelphia County, Pennsylvania 2646.71 9.26 1347.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1360.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 NaN NaN NaN NaN 0.0 0.0 0.0 2646.71 9.26 1374.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 21.0 19.0 1.38 1.53 1.0 0.0 0.0 2646.71 9.26 1388.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 21.0 1.51 1.8 1.0 0.0 0.0 2646.71 9.26 1401.0 77.12 959.0 48886.0 189700.0 24.5 78.45 12.42 3.47 0.23 3.92 0.0 1.4 0.11 25.0 24.0 1.71 1.78 1.0 0.0 0.0 3310.88 12.11 1415.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 18.0 15.0 1.06 1.27 1.0 0.0 0.0 3310.88 12.11 1428.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 13.0 10.0 0.7 0.91 1.0 0.0 0.0 3310.88 12.11 1442.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 53.0 20.0 1.39 3.68 0.0 0.0 1.0 3310.88 12.11 1456.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 30.0 17.0 1.17 2.06 0.0 0.0 1.0 3310.88 12.11 1469.0 57.97 1357.0 73272.0 332500.0 25.6 83.98 7.24 4.4 0.0 3.08 0.0 0.45 0.84 25.0 11.0 0.75 1.7 0.0 0.0 1.0 3478.0 3.13 1483.0 64.45 1491.0 75505.0 340800.0 27.5 83.09 5.95 3.62 0.14 4.97 0.06 2.01 0.14 24.0 18.0 1.21 1.62 0.0 0.0 1.0 3608.0 0.0 1524.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 23.0 11.0 0.72 1.51 0.0 0.0 1.0 3608.0 0.0 1565.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 22.0 7.0 0.45 1.41 0.0 0.0 1.0 3608.0 0.0 1606.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 12.0 0.75 1.56 0.0 0.0 1.0 3608.0 0.0 1646.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 26.0 12.0 0.73 1.58 0.0 0.0 1.0 3608.0 0.0 1687.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 31.0 12.0 0.71 1.84 0.0 0.0 1.0 3608.0 0.0 1728.0 71.19 1545.0 92207.0 351600.0 24.3 73.45 10.01 5.1 0.17 8.79 0.0 2.49 0.0 25.0 16.0 0.93 1.45 0.0 0.0 1.0 MULTIPOLYGON (((-75.14161 39.95549, -75.14163 ... EXT A-CLEAN WEEDS/PLANTS 0
# Create interactive choropleths for the 20 most common violations
choropleth_violations_20 = violations_by_tracts_20.hvplot.polygons(
    geo=True,
    tiles='CartoLight',
    color='N',
    groupby='violationdescription',
    dynamic=False,
    frame_width=600,
    frame_height=600,
    cmap='Viridis',
    hover_cols=['GEOID', 'N']
)

# Display the interactive choropleth for the 20 most common violations
choropleth_violations_20

Part 2: Exploring the NDVI in Philadelphia

In this part, we’ll explore the NDVI in Philadelphia a bit more. This part will include two parts:

  1. We’ll compare the median NDVI within the city limits and the immediate suburbs
  2. We’ll calculate the NDVI around street trees in the city.

2.1 Comparing the NDVI in the city and the suburbs

2.1.1 Load Landsat data for Philadelphia

Use rasterio to load the landsat data for Philadelphia (available in the “data/” folder)

import rasterio as rio
landsat = rio.open("./data/landsat8_philly.tif")

landsat
<open DatasetReader name='./data/landsat8_philly.tif' mode='r'>
# The CRS
landsat.crs

# The bounds
landsat.bounds

# The number of bands available
landsat.count

# The band numbers that are available
landsat.indexes

# Number of pixels in the x and y directions
landsat.shape

# The 6 parameters that map from pixel to real space
landsat.transform

# All of the meta data
landsat.meta
{'driver': 'GTiff',
 'dtype': 'uint16',
 'nodata': None,
 'width': 923,
 'height': 999,
 'count': 10,
 'crs': CRS.from_epsg(32618),
 'transform': Affine(30.0, 0.0, 476064.3596176505,
        0.0, -30.0, 4443066.927074196)}

2.1.2 Separating the city from the suburbs

Create two polygon objects, one for the city limits and one for the suburbs. To calculate the suburbs polygon, we will take everything outside the city limits but still within the bounding box.

  • The city limits are available in the “data/” folder.
  • To calculate the suburbs polygon, the “envelope” attribute of the city limits geometry will be useful.
  • You can use geopandas’ geometric manipulation functionality to calculate the suburbs polygon from the city limits polygon and the envelope polygon.
# Load data and convert to the correct CRS
city_limits = gpd.read_file("data/City_Limits.geojson")
city_limits = city_limits.to_crs(epsg=landsat.crs.to_epsg())
city_limits.head()
OBJECTID Shape__Area Shape__Length geometry
0 1 0.038911 1.259687 POLYGON ((498724.960 4443066.927, 498759.458 4...
import shapefile
import matplotlib.pyplot as plt
from shapely.geometry import shape, Point
from shapely.geometry import Polygon
# Get the city limits polygon
city_polygon = city_limits.geometry.iloc[0]

# Calculate the bounding box for the city limits
bounding_box = city_polygon.envelope

# Calculate the suburbs polygon as the difference between the bounding box and the city limits
suburbs_polygon = bounding_box.difference(city_polygon)

# Create GeoDataFrame for suburbs
suburbs_gdf = gpd.GeoDataFrame(
    {
        'name': ['Suburbs'],
        'geometry': [suburbs_polygon]
    },
    crs=city_limits.crs
)

suburbs_gdf.head()
name geometry
0 Suburbs MULTIPOLYGON (((476064.360 4413069.557, 476064...

2.1.3 Mask and calculate the NDVI for the city and the suburbs

Using the two polygons from the last section, use rasterio’s mask functionality to create two masked arrays from the landsat data, one for the city and one for the suburbs.

For each masked array, calculate the NDVI.

Mask

from rasterio.mask import mask
# Mask the city area
city_masked, city_transform = mask(
    dataset=landsat,               # The original raster data
    shapes=[city_polygon],         # city
    crop=True,                     # remove pixels not within boundary
    all_touched=True,              # get all pixels that touch the boudnary
    filled=False                   # do not fill cropped pixels with a default value
)

# Mask the suburb area
suburbs_masked, suburbs_transform = mask(
    dataset=landsat,               
    shapes=[suburbs_polygon],      # suburb
    crop=True,                     
    all_touched=True,              
    filled=False                   
)

Calculate NDVI for city and suburbs

Formula: NDVI = (NIR - Red) / (NIR + Red)

NIR is band 5 and Red is band 4, but the indexing here is zero-based, e.g., band 5 is index 4

city_red = city_masked[3]  
city_nir = city_masked[4]  

suburbs_red = suburbs_masked[3]  
suburbs_nir = suburbs_masked[4]  
def calculate_NDVI(city_nir, city_red):
    """
    Calculate the NDVI from the NIR and red landsat bands
    """

    # Convert to floats
    city_nir = city_nir.astype(float)
    city_red = city_red.astype(float)

    # Get valid entries
    city_check = np.logical_and(city_red.mask == False, city_nir.mask == False)

    # Where the check is True, return the NDVI, else return NaN
    city_ndvi = np.where(city_check, (city_nir - city_red) / (city_nir + city_red), np.nan)
    
    # Return
    return city_ndvi
def calculate_NDVI(suburbs_nir, suburbs_red):
    """
    Calculate the NDVI from the NIR and red landsat bands
    """

    # Convert to floats
    suburbs_nir = suburbs_nir.astype(float)
    suburbs_red = suburbs_red.astype(float)

    # Get valid entries
    suburbs_check = np.logical_and(suburbs_red.mask == False, suburbs_nir.mask == False)

    # Where the check is True, return the NDVI, else return NaN
    suburbs_ndvi = np.where(suburbs_check, (suburbs_nir - suburbs_red) / (suburbs_nir + suburbs_red), np.nan)
    
    # Return
    return suburbs_ndvi
city_NDVI = calculate_NDVI(city_nir, city_red)
suburbs_NDVI = calculate_NDVI(suburbs_nir, suburbs_red)

City

# The extent of the data
landsat_extent = [
    landsat.bounds.left,
    landsat.bounds.right,
    landsat.bounds.bottom,
    landsat.bounds.top,
]
fig, ax = plt.subplots(figsize=(8, 8))

# Plot NDVI
city_img = ax.imshow(city_NDVI, extent=landsat_extent)

# Format and plot city limits
city_limits.plot(ax=ax, edgecolor="gray", facecolor="none", linewidth=2)
plt.colorbar(city_img)
ax.set_axis_off()
ax.set_title("NDVI in Philadelphia", fontsize=18);

Suburbs

fig, ax = plt.subplots(figsize=(8, 8))

# Plot NDVI
suburbs_img = ax.imshow(suburbs_NDVI, extent=landsat_extent)

# Format and plot suburbs
suburbs_gdf.plot(ax=ax, edgecolor="gray", facecolor="none", linewidth=2)
plt.colorbar(suburbs_img)
ax.set_axis_off()
ax.set_title("NDVI in Suburbs", fontsize=18);

2.1.4 Calculate the median NDVI within the city and within the suburbs

  • Calculate the median value from your NDVI arrays for the city and suburbs
  • Numpy’s nanmedian function will be useful for ignoring NaN elements
  • Print out the median values. Which has a higher NDVI: the city or suburbs?
from rasterstats import zonal_stats

The Median NDVI within the City

city_stats = zonal_stats(
    city_limits,  # The vector data
    city_NDVI,  # The array holding the raster data
    affine=landsat.transform,  # The affine transform for the raster data
    stats=["median"],  # The stats to compute
    nodata=np.nan,  # Missing data representation
)

# Extract median NDVI values from the stats result
median_city = [stats_dict["median"] for stats_dict in city_stats]

# Store the median NDVI values in the original GeoDataFrame
city_limits["median_NDVI"] = median_city

city_limits.head()
OBJECTID Shape__Area Shape__Length geometry median_NDVI
0 1 0.038911 1.259687 POLYGON ((498724.960 4443066.927, 498759.458 4... 0.202466

The Median NDVI within the Suburbs

suburbs_stats = zonal_stats(
    suburbs_gdf,  # The vector data
    suburbs_NDVI,  # The array holding the raster data
    affine=landsat.transform,  # The affine transform for the raster data
    stats=["median"],  # The stats to compute
    nodata=np.nan,  # Missing data representation
)

# Extract median NDVI values from the stats result
median_suburbs = [stats_dict["median"] for stats_dict in suburbs_stats]

# Store the median NDVI values in the original GeoDataFrame
suburbs_gdf["median_NDVI"] = median_suburbs

suburbs_gdf.head()
name geometry median_NDVI
0 Suburbs MULTIPOLYGON (((476064.360 4413069.557, 476064... 0.374849

Compare

city_median_ndvi = city_limits['median_NDVI'].iloc[0]
print(f"Median NDVI in the city: {city_median_ndvi:.4f}")

suburbs_median_ndvi = suburbs_gdf['median_NDVI'].iloc[0]
print(f"Median NDVI in the Suburbs: {suburbs_median_ndvi:.4f}")

# Determine which has higher NDVI
if city_median_ndvi > suburbs_median_ndvi:
    print("The city has a higher median NDVI than the suburbs.")
elif city_median_ndvi < suburbs_median_ndvi:
    print("The suburbs have a higher median NDVI than the city.")
else:
    print("The city and the suburbs have the same median NDVI.")
Median NDVI in the city: 0.2025
Median NDVI in the Suburbs: 0.3748
The suburbs have a higher median NDVI than the city.

2.2 Calculating the NDVI for Philadelphia’s street treets

2.2.1 Load the street tree data

The data is available in the “data/” folder. It has been downloaded from OpenDataPhilly. It contains the locations of abot 2,500 street trees in Philadelphia.

# Load data and convert to the correct CRS
street_trees = gpd.read_file("data/ppr_tree_canopy_points_2015.geojson")
street_trees = street_trees.to_crs(epsg=landsat.crs.to_epsg())
street_trees.head()
objectid fcode geometry
0 1 3000 POINT (499541.269 4434698.265)
1 2 3000 POINT (488932.471 4424093.158)
2 3 3000 POINT (489039.214 4425985.827)
3 4 3000 POINT (488993.171 4426088.005)
4 5 3000 POINT (488943.113 4424599.478)

2.2.2 Calculate the NDVI values at the locations of the street trees

  • Use the rasterstats package to calculate the NDVI values at the locations of the street trees.
  • Since these are point geometries, you can calculate either the median or the mean statistic (only one pixel will contain each point).
stats_by_trees = zonal_stats(
    street_trees,  # The vector data
    city_NDVI,  # The array holding the raster data
    affine=landsat.transform,  # The affine transform for the raster data
    stats=["median"],  # The stats to compute
    nodata=np.nan,  # Missing data representation
)

# Extract median NDVI values from the stats result
median_stats = [stats_dict["median"] for stats_dict in stats_by_trees]

# Store the median NDVI values in the original GeoDataFrame
street_trees["median_NDVI"] = median_stats

street_trees.head()
objectid fcode geometry median_NDVI
0 1 3000 POINT (499541.269 4434698.265) 0.235337
1 2 3000 POINT (488932.471 4424093.158) 0.261535
2 3 3000 POINT (489039.214 4425985.827) 0.096769
3 4 3000 POINT (488993.171 4426088.005) 0.076630
4 5 3000 POINT (488943.113 4424599.478) 0.267952

2.2.3 Plotting the results

Make two plots of the results:

  1. A histogram of the NDVI values, using matplotlib’s hist function. Include a vertical line that marks the NDVI = 0 threshold
  2. A plot of the street tree points, colored by the NDVI value, using geopandas’ plot function. Include the city limits boundary on your plot.

The figures should be clear and well-styled, with for example, labels for axes, legends, and clear color choices.

A histogram of the NDVI values

# Initialize
fig, ax = plt.subplots(figsize=(8, 6))

# Plot a histogram
ax.hist(street_trees["median_NDVI"], bins="auto")
ax.axvline(x=0, c="k", lw=2)

# Format
ax.set_xlabel('Median NDVI', fontsize=14)
ax.set_ylabel('Number of Trees', fontsize=14)
ax.set_title('Histogram of Median NDVI at Street Tree Locations', fontsize=16)
Text(0.5, 1.0, 'Histogram of Median NDVI at Street Tree Locations')

As can be seen from the histogram, the NDVI values of most street tree locations are concentrated in the range of 0 to 0.3, among which the number of trees with NDVI values of 0 to 0.1 is the largest, and the frequency is about 300. This suggests that there is generally less vegetation cover in the area where street trees are located. Only a few street trees had an NDVI value of more than 0.4, indicating that the surrounding vegetation was healthy. In general, the vegetation health status of street trees has a large room for improvement, especially in those areas with low NDVI value, so it may be necessary to take measures to improve the vegetation quality.

A plot of the street tree points

# Initialize
fig, ax = plt.subplots(figsize=(8, 8))

# Plot the city limits
city_limits.plot(ax=ax, edgecolor="black", facecolor="none", linewidth=4)

# Plot the median NDVI
street_trees.plot(column="median_NDVI", legend=True, ax=ax, cmap="viridis")

# Format
ax.set_axis_off()
ax.set_title('Street Trees Colored by NDVI Value', fontsize=16)
Text(0.5, 1.0, 'Street Trees Colored by NDVI Value')

As can be seen from the figure, there are obvious differences in the NDVI values of street trees in different urban regions: the northern and northeastern regions have higher NDVI values and better vegetation cover, while the central and southern regions have lower NDVI values and relatively poor vegetation cover. This suggests that the environment around street trees is more favourable in the north and North-East, while more green improvements are needed in the centre and south to improve vegetation health and coverage.